package com.gupaoedu.zookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * 腾讯课堂搜索 咕泡学院
 * 加群获取视频：608583947
 * 风骚的Michael 老师
 */
public class ConnectionDemo {

    public static void main(String[] args) {
        try {
            final CountDownLatch countDownLatch=new CountDownLatch(1);
            ZooKeeper zooKeeper=
                    new ZooKeeper("192.168.187.128:2181,192.168.187.129:2181,192.168.187.130:2181",
                            4000, new Watcher() {
                        @Override
                        public void process(WatchedEvent event) {   //这里是watch机制，文尾有其它写法
                            if(Event.KeeperState.SyncConnected==event.getState()){
                                //如果收到了服务端的响应事件，连接成功
                                countDownLatch.countDown();
                            }
                        }
                    });
            countDownLatch.await();     //watch得知启动成功，再释放
            System.out.println(zooKeeper.getState());  //CONNECTED

            //添加节点
            //参数说明：持久化节点、value、当前节点的权限控制（目前是任何人都可以访问）、表示当前节点是持久化节点
            zooKeeper.create("/zk-persis-mic","0".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
            Thread.sleep(1000);
            Stat stat=new Stat();

            //得到当前节点的值
            byte[] bytes=zooKeeper.getData("/zk-persis-mic",null,stat); //这里会更新stat
            System.out.println(new String(bytes));

            //修改节点值
            // 这里的version相当于乐观锁；  stat相当于linux上的get /mic， 获取节点的状态信息，包括zxid，cversion等
            zooKeeper.setData("/zk-persis-mic","1".getBytes(),stat.getVersion());

            //得到当前节点的值
            byte[] bytes1=zooKeeper.getData("/zk-persis-mic",null,stat);  //这里会更新stat
            System.out.println(new String(bytes1));

            zooKeeper.delete("/zk-persis-mic",stat.getVersion());

            zooKeeper.close();

            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }
    
    /*
    * ,new Watcher(){
    *       @Override
    *       public void process(WatcherEvent event){
    *           if(Event.KeeperState.SyncConnected==event.getState()){
    *                countDownLatch.countDown();
    *                }
    *        }
    * }
    *
    * */
}
